﻿using JinJun.YShow.BLL.IService;
using JinJun.YShow.Model;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace JinJun.YShow.BLL.Service
{
    public class CategoryService:BaseService<Category>,ICategoryService
    {
        public bool DeleteCategory(Category category)
        {
            Expression<Func<Category, bool>> lambdaCategory = a => a.ParentId==category.Id;
            if (Exists(lambdaCategory))
            {
                return false;
            }
            //创建实例
            using (var db = new YShowDBEntities())
            {
                using (var tran = db.Database.BeginTransaction())
                {
                    try
                    {
                        db.Entry(category).State = EntityState.Deleted;
                        //保存
                        db.SaveChanges();
                        //提交
                        tran.Commit();
                    }
                    catch (Exception ex)
                    {
                        //回滚
                        tran.Rollback();
                        return false;
                    }
                }
            }
            return true;
        } 
    }
}
